package com.itjiange.servlet;

import com.itjiange.service.DeptService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/delete")
public class DeleteServlet extends HttpServlet {
    private DeptService deptService = new DeptService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取要删除的对象的某一个值
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        Integer deptno = Integer.parseInt(req.getParameter("id"));
        System.out.println(deptno);
        int deleted = deptService.delete(deptno);
        if (deleted == -1) {
            resp.getWriter().write("<h1>删除失败</h1>");
            return;
        }
        resp.getWriter().write("<h1>删除成功</h1>");
        resp.sendRedirect("/all");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
